package com.lollipop.synthential.java8.java8time;

import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @Author: J.K
 * @Date: 2021-03-06 14:54
 * @Description: Java8全新的日期时间API-不变类型，线程安全 simpledateformat线程不安全，阿里巴巴开发手册建议使用java.time的时间处理api
 */
@Slf4j
public class DateTimeProcessorDemo {
    public static void main(String[] args) {
        // 1.获取当前日期
        LocalDate today = LocalDate.now();
        log.info("当前日期：{}", today);

        // 2.分别获取年月日
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        log.info("year：{},month：{},day：{}", year, month, day);

        // 3.自定义日期
        LocalDate customDate = LocalDate.of(2020, 11, 30);
        log.info("自定义的日期：{}", customDate);

        // 4.判断2个日期是否相等
        LocalDate localDate1 = LocalDate.now();
        LocalDate localDate2 = LocalDate.of(2021, 3, 6);
        if (localDate1.equals(localDate2)) {
            log.info("两个日期相等：{}", localDate1);
        } else {
            log.info("两个日期不等");
        }

        // 5.检查生日周期性时间
        MonthDay birthday = MonthDay.of(localDate2.getMonth(), localDate2.getDayOfMonth());
//        MonthDay birthday = MonthDay.of(customDate.getMonth(), customDate.getDayOfMonth());
        MonthDay curmonthday = MonthDay.from(today);
        if (curmonthday.equals(birthday)) {
            log.info("今天是你的生日：{}", birthday);
        } else {
            log.info("今天不是你的生日，你的生日是：{}", birthday);
        }


        // 6.获取当前时间
        LocalDateTime now = LocalDateTime.now();
        log.info("当前时间：{}", now);

        // 7.计算3小时后的时间
        LocalDateTime time = now.plusHours(3);
        log.info("3小时候的时间：{}", time);

        // 8.计算一周后的时间
        LocalDateTime weekTime = now.plusWeeks(1);
        LocalDateTime weekTime2 = now.plus(1, ChronoUnit.WEEKS);
        log.info("一周后的时间：{}", weekTime);
        log.info("一周后的时间：{}", weekTime2);


        // 9.计算1年前、1年后的时间
        LocalDateTime previousYear = now.minus(1, ChronoUnit.YEARS);
        LocalDateTime nextYear = now.plus(1, ChronoUnit.YEARS);
        log.info("去年的当前时间：{}<--->明年的当前时间：{}", previousYear, nextYear);

        // 10.Clock时钟类，用于获取当前时间的时间戳，或当前时区下的时间信息
        Clock clock = Clock.systemUTC();
        Clock defaultCloock = Clock.systemDefaultZone();
        log.info("clock：{}<-->defaultClock：{}", clock.millis(), defaultCloock.millis());

        // 11.判断2个日期的早晚
        log.info("isAfter：{}", customDate.isAfter(today));
        log.info("isBefore：{}", customDate.isBefore((today)));
        log.info("isEqual：{}", customDate.isEqual((today)));

        // 12.处理时区
        ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime localtDateAndTime = LocalDateTime.now();
        ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america);
        log.info("Current date and time in a particular timezone : {}", dateAndTimeInNewYork);

        // 13.表示信用卡到期这类固定日期
        YearMonth currentYearMonth = YearMonth.now();
        YearMonth previouMonth = currentYearMonth.minus(1, ChronoUnit.MONTHS);
        YearMonth creditCardExpire = YearMonth.of(2020, Month.FEBRUARY);
        log.info("Days in month {},{}", previouMonth, previouMonth.lengthOfMonth());
        log.info("Your credit card exipires on {}", creditCardExpire);

        // 14.检查闰年
        log.info("本年是否为闰年：{}", today.isLeapYear());

        // 15.计算两个日期间的天数和月数
        Period period = Period.between(today, customDate);
        log.info("日期差：{}", period.getMonths());

        // 16.获取当前的时间戳
        Instant timestamp = Instant.now();
        log.info("当前时间戳：{}", timestamp.toEpochMilli());

        // 17.使用预定义的格式化工具去解析或格式化日期
        String dateStr = "20201128";
        LocalDate parseDate = LocalDate.parse(dateStr, DateTimeFormatter.BASIC_ISO_DATE);
        log.info("{}格式化后的日期：{}", dateStr, parseDate);

        // 18.字符串互转日期类型
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        // 日期->字符串
        String timeStr = now.format(dateTimeFormatter);
        LocalDate date = LocalDate.parse(timeStr, dateTimeFormatter);
        log.info("日期转字符串：{}", timeStr);
        log.info("字符串转日期：{}", date);
    }
}
